package com.googlecode.perfectable.visuable.parse;

import static com.google.common.base.Preconditions.checkState;

import java.util.LinkedList;
import java.util.List;

import com.google.common.base.Optional;

class VisualStructureBuilder {
	private final Optional<ElementStructureBuilder> elementBuilder;
	private List<ElementStructure> created = new LinkedList<>();

	private VisualStructureBuilder() {
		this.elementBuilder = Optional.absent();
	}

	VisualStructureBuilder(ElementStructureBuilder elementStructureBuilder) {
		this.elementBuilder = Optional.of(elementStructureBuilder);
	}

	public static VisualStructureBuilder start() {
		return new VisualStructureBuilder();
	}

	public ElementStructureBuilder element(String elementName) {
		return new ElementStructureBuilder(this, elementName);
	}

	public Iterable<ElementStructure> build() {
		return this.created;
	}

	public ElementStructureBuilder endChildren() {
		checkState(this.elementBuilder.isPresent());
		ElementStructureBuilder elementStructureBuilder = this.elementBuilder.get();
		elementStructureBuilder.children(this.build());
		return elementStructureBuilder;
	}

	public void add(ElementStructure structure) {
		this.created.add(structure);
	}
}